/*  

Cafechess Library: Cover basic chess programming needs, like GUI, Engine integration, 
FEN and PGN parsing.

                 \\|//
                 (o o)
--------------ooO-(_)-Ooo----------------------------------
Copyright (C) 2009  By Gregory A. Prentice Snoqualmie, WA
                    www.cafechess.org
                    cafechess@comcast.net
                      Oooo
--------------oooO----(  )---------------------------------
              (  )    (_/
               \_)

Microsoft Public License (Ms-PL)
               
Revisions:

Author        Date    Description:

*/
using System;
using System.Drawing;
using System.Drawing.Imaging;

using Cafechess.General;

namespace Cafechess.Imaging
{
	/// <summary>
	/// A bitmap that implements our IDrawable interface.  Can handle calling
	/// the Bitmap.Dispose() method if we mark IsResourceOwner as true.
	/// </summary>
  public class DrawableBitmap : Disposable, IDrawable
  {
    #region Fields
    private Bitmap coDrawing;
    bool coIsEnabled;
    bool coIsRedraw;
    bool coIsDragging;
    bool coIsResourceOwner;
    bool coIsLocked;
    string coName;
    private Rectangle coSquare;
    ImageAttributes coImageAttributes;
    IDrawable coParent;
    #endregion

    #region Properties
    /// <summary>
    /// The Bitmap this object owns.
    /// </summary>
    public Bitmap Drawing
    {
      get{ return coDrawing; }
      set{ coDrawing = value; }
    }
    /// <summary>
    /// The bounding rectangle of this image.
    /// </summary>
    public Rectangle Square
    {
      get{ return coSquare; }
      set{ coSquare = value; }
    }
    /// <summary>
    /// Enables this item to render itself.  Shares the same 
    /// member field as IsRedraw.
    /// </summary>
    public bool IsEnabled
    { 
      get{ return coIsEnabled; } 
      set{ coIsEnabled = value; } 
    }
    /// <summary>
    /// Enables this item to render itself.  Shares the same 
    /// member field as IsEnabled.
    /// </summary>
    public bool IsRedraw
    {
      get{ return coIsRedraw; } 
      set{ coIsRedraw = value; } 
    }
    public bool IsDragging
    {
      get{return coIsDragging;}
      set{coIsDragging = value;}
    }
    /// <summary>
    /// Used to lock the state of this layer from any changes or movement.
    /// </summary>
    public bool IsLocked
    { 
      get{ return coIsLocked; } set{ coIsLocked = value;} 
    }

    /// <summary>
    /// Used to determine if we own the bitmap, if we
    /// do then it will be clean up when we Dispose of this
    /// object explicitly or implicitly.
    /// </summary>
    public bool IsResouceOwner
    {
      get{return coIsResourceOwner;}
      set{coIsResourceOwner = value;}
    }
    #endregion Properties 

    #region Constructors
    public DrawableBitmap(string name)
    {
      Name = name;
      IsEnabled = true;
      IsRedraw = true;
      coIsResourceOwner = false;
      coImageAttributes = new ImageAttributes();
      coImageAttributes.SetColorKey(Color.Transparent,Color.Transparent);

    }
    public DrawableBitmap(string name, Bitmap bm, Rectangle square)
    {
      Name = name;
      Drawing = bm;
      Square = square;
      IsEnabled = true;

      coIsResourceOwner = false;
      coImageAttributes = new ImageAttributes();
      coImageAttributes.SetColorKey(Color.Transparent,Color.Transparent);

    }
    #endregion Constructors

    #region IDrawable Members

    public string Name
    {
      get{ return coName; }
      set{ coName = value; }
    }
    virtual public void Clear()
    {
      Drawing = null;
    }

    public IDrawable Parent
    { get{return coParent;} set{coParent=value;}}
    
    public bool HitTest(Point point)
    {
      return Square.Contains(point);
    }
   
    public void Render(Graphics graphics)
    {
      if(IsEnabled && coDrawing != null && IsRedraw)
      {
#if POCKET_PC
        graphics.DrawImage(coDrawing,Square,new Rectangle(0,0,coDrawing.Size.Width,coDrawing.Size.Height),GraphicsUnit.Pixel);    
#else
        graphics.DrawImage(coDrawing,Square);
        //graphics.DrawImage(coDrawing, Square,Square.X,Square.Y,Square.Width,Square.Height,GraphicsUnit.Pixel,coImageAttributes);
#endif
      }
    }

    public void Render(Graphics graphics, bool force)
    {
      bool save = coIsEnabled;
      coIsEnabled = true;
      Render(graphics);
      coIsEnabled = save;
    }


    #endregion

    #region Methods
    /// <summary>
    /// Clean up any managed code.
    /// </summary>
    override protected void DisposeManaged()
    {
      if( ! IsDisposed )
      {

      }
    }
    /// <summary>
    /// Clean up our bitmap if we own it.
    /// </summary>
    override protected void DisposeUnmanaged()
    {
      if( ! IsDisposed )
      {
        if(IsResouceOwner)
        {
          Drawing.Dispose();
          Drawing = null;
        }

      }
    }

    #endregion Methods
  }
}
